package com.hemaapp.yjnh.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.hemaapp.yjnh.BaseAdapter;
import com.hemaapp.yjnh.R;
import com.hemaapp.yjnh.listener.OnItemClickListener;
import com.hemaapp.yjnh.bean.FollowCollect;
import com.hemaapp.yjnh.utils.ImageUtils;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 我的关注列表适配器
 *
 * @author GuiyouZhang
 */
public class AttentionAdapter extends BaseAdapter {

    private ArrayList<FollowCollect> mDatas;
    private boolean isEdit = false;
    private OnItemClickListener clickListener;
    public int type = 1;// 1非田家，2田家

    public AttentionAdapter(Context mContext, ArrayList<FollowCollect> followCollects, int type) {
        super(mContext);
        this.mDatas = followCollects;
        this.type = type;
    }

    public void setEdit(boolean edit) {
        isEdit = edit;
    }

    public void setClickListener(OnItemClickListener clickListener) {
        this.clickListener = clickListener;
    }

    @Override
    public int getCount() {
        int size = mDatas == null ? 0 : mDatas.size();
        return size == 0 ? 1 : mDatas.size();
    }

    @Override
    public boolean isEmpty() {
        int size = mDatas == null ? 0 : mDatas.size();
        return size == 0;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (isEmpty())
            return getEmptyView(parent);
        if (type == 1) {
            ViewHolder1 holder;
            if (convertView == null || convertView.getTag(R.id.TAG_VIEWHOLDER) == null) {
                convertView = LayoutInflater.from(mContext).inflate(
                        R.layout.item_attention_production, null);
                holder = new ViewHolder1(convertView);
                convertView.setTag(R.id.TAG_VIEWHOLDER, holder);
            } else {
                holder = (ViewHolder1) convertView.getTag(R.id.TAG_VIEWHOLDER);
            }
            holder.setDatas(position);
            return convertView;
        } else {
            ViewHolder2 holder;
            if (convertView == null || convertView.getTag(R.id.TAG_VIEWHOLDER) == null) {
                convertView = LayoutInflater.from(mContext).inflate(
                        R.layout.item_attention_farmer, null);
                holder = new ViewHolder2(convertView);
                convertView.setTag(R.id.TAG_VIEWHOLDER, holder);
            } else {
                holder = (ViewHolder2) convertView.getTag(R.id.TAG_VIEWHOLDER);
            }
            holder.setDatas(position);
            return convertView;
        }
    }

    class ViewHolder1 {
        @Bind(R.id.iv_bkg)
        ImageView ivBkg;
        @Bind(R.id.iv_selector)
        ImageView ivSelector;
        @Bind(R.id.tv_name)
        TextView tvName;
        @Bind(R.id.ratingBar)
        RatingBar ratingBar;
        int position = -1;

        ViewHolder1(View view) {
            ButterKnife.bind(this, view);
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (clickListener != null)
                        clickListener.onClickListener(position);
                }
            });
        }

        private void setDatas(int position) {
            this.position = position;
            FollowCollect client = mDatas.get(position);
            ImageUtils.loadRadiusImage(mContext, client.getAvatar(), ivBkg, 10);

            String star = client.getStarscore();
            if (!TextUtils.isEmpty(star)) {
                ratingBar.setRating(Float.parseFloat(star));
            }
            tvName.setText(client.getNickname());
            if (isEdit) {
                ivSelector.setVisibility(View.VISIBLE);
                if (client.isCheck())
                    ivSelector.setImageResource(R.drawable.select_y_orange);
                else
                    ivSelector.setImageResource(R.drawable.select_n);
            }else
                ivSelector.setVisibility(View.GONE);
        }
    }

    class ViewHolder2 {
        @Bind(R.id.iv_bkg)
        ImageView ivBkg;
        @Bind(R.id.iv_selector)
        ImageView ivSelector;
        @Bind(R.id.iv_avatar)
        ImageView ivAvatar;
        @Bind(R.id.tv_name)
        TextView tvName;
        @Bind(R.id.ratingBar)
        RatingBar ratingBar;
        @Bind(R.id.tv_content)
        TextView tvContent;
        int position = -1;

        ViewHolder2(View view) {
            ButterKnife.bind(this, view);
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (clickListener != null)
                        clickListener.onClickListener(position);
                }
            });
        }

        private void setDatas(int position) {
            this.position = position;
            FollowCollect client = mDatas.get(position);
            ImageUtils.loadAvatar(mContext, client.getAvatar(), ivAvatar);
            ImageUtils.loadRadiusImageMid(mContext, client.getImgurl(), ivBkg, 10);

            String star = client.getStarscore();
            if (!TextUtils.isEmpty(star)) {
                ratingBar.setRating(Float.parseFloat(star));
            }
            tvName.setText(client.getNickname());
            if (isNull(client.getGood_names()))
                tvContent.setText("田家没有提供商品定制");
            else
                tvContent.setText("田家推出"+client.getGood_names()+"等商品定制");
            if (isEdit) {
                ivSelector.setVisibility(View.VISIBLE);
                if (client.isCheck())
                    ivSelector.setImageResource(R.drawable.select_y_orange);
                else
                    ivSelector.setImageResource(R.drawable.select_n);
            }else
                ivSelector.setVisibility(View.GONE);
        }
    }
}
